#encoding:utf-8

class God(object):
    def __init__(self, world=[[]]):
        self.world = world
        self.round = 0
    
    def fly(self, obj, *arg):
        position = obj.position
        if arg[0]>=len(self.world) or arg[1]>=len(self.world[0]):
            return False
        elif self.world[arg[0]][arg[1]] != 0:
            return False
        self.world[position[0]][position[1]] = 0
        self.world[arg[0]][arg[1]] = obj
        obj.position = arg
        return True
        
    def filt(self):
        '''遍历地图,得到每个点的obj和坐标'''
        for i,height in enumerate(self.world):
            for j,obj in enumerate(height):
                if type(obj)!=type(0) and obj.hp!=0:
                    yield obj,i,j
    
    def getPosition(self,ppl):
        '''获取角色在地图上的坐标'''
        for obj,i,j in self.filt():
            if obj == ppl:
                ppl.position = (i,j)
                return i,j
    
    @property
    def objOrderList(self):
        return sorted(self.filt(),key=lambda x:x[0].dexterity,reverse=True)
        
    def missionStart(self):
        self.round += 1
    
    def missionEnd(self):
        if not [obj for obj,x,y in self.objOrderList if obj.camp==1]:
            return 1
        if not [obj for obj,x,y in self.objOrderList if obj.camp==0]:
            return 2
        
    def nextObj(self):
        for obj,x,y in self.objOrderList:
            if obj.end == 2:
                obj.end = 1
                return obj
    
    def nextRound(self):
        for obj,x,y in self.objOrderList:
            obj.end = 2
        self.round += 1
                
    def __setitem__(self, key, item):
        self.world[key[0]][key[1]] = item
        item.position = key
        item.god = self

    def __getitem__(self, key):
        return self.world[key[0]][key[1]]